package com.zboot.zbootadmin.project.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zboot.zbootadmin.common.utils.PagingResult;
import com.zboot.zbootadmin.project.base.command.machinery.MachineryModelTypeListCommand;
import com.zboot.zbootadmin.project.base.entity.MachineryModelType;
import com.zboot.zbootadmin.project.base.mappers.MachineryModelTypeMapper;
import com.zboot.zbootadmin.project.base.service.MachineryModelTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author draw
 * @since 2021-05-08
 */
@Service
public class MachineryModelTypeServiceImpl extends ServiceImpl<MachineryModelTypeMapper, MachineryModelType> implements MachineryModelTypeService {
    @Autowired
    private MachineryModelTypeMapper machineryModelTypeMapper;

    @Override
    public void updateToDelete(Long id) {
        machineryModelTypeMapper.updateToDelete(id);
    }

    @Override
    public PagingResult pageList(MachineryModelTypeListCommand command) {
        Page pageVo = new Page(command.getPage(), command.getPageSize());
        LambdaQueryWrapper<MachineryModelType> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(command.getName())) {
            queryWrapper.like(MachineryModelType::getName, command.getName());
        }

        if (Objects.nonNull(command.getStatus())) {
            queryWrapper.eq(MachineryModelType::getStatus, command.getStatus());
        }
        queryWrapper.orderByDesc(MachineryModelType::getCreateDate);
        IPage<MachineryModelType> iPage = machineryModelTypeMapper.selectPage(pageVo, queryWrapper);
        PagingResult pagingResult = new PagingResult();
        pagingResult.setItems(iPage.getRecords());
        pagingResult.setTotalCount(iPage.getTotal());
        return pagingResult;
    }
}
